package com.fruits.util;

import com.fruits.login.model.ReturnMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 参数检测
 */
public class ParameterDetectionUtil {

    private static final Logger logger = LoggerFactory.getLogger(ParameterDetectionUtil.class);

    public static ReturnMsg checkPhone(String phone){
        ReturnMsg msg = new ReturnMsg();
        msg.setState(0);
        if (phone == null){
            logger.info("电话号码不能为空");
            msg.setInfo("电话号码不能为空");
            return msg;
        }
        Pattern p = Pattern.compile(RegexProperties.REGEX_PHONE);
        Matcher m = p.matcher(phone);
        if (!m.matches()){
            msg.setInfo("电话号码格式不正确");
            logger.info(phone + "电话号码格式不正确");
            return msg;
        }
        msg.setState(1);
//        logger.info("电话号码验证成功");
        return msg;
    }

    /**
     * 判断 number 是否是靓号,是返回T,否则返回F
     * 目前判断有:
     * 1.三位相同:AAA
     * 2.四位以上顺增:1234
     * 3.四位以上顺减:4321
     * @param number
     * @return
     */
    public static boolean isGoodNumber(String number){
        Pattern p = Pattern.compile(RegexProperties.PLAYERID_NUMBER);
        Matcher m = p.matcher(number);
        if (m.matches()){
            logger.info(number + "位数正确");
        }else {
            //位数不正确时 暂且 按照靓号处理:则重新生成
            logger.info(number + "位数不正确");
            return true;
        }
        p = Pattern.compile(RegexProperties.BEAUTIFUL_THREE_ALIKE);
        m = p.matcher(number);
        if (m.matches()){
            logger.info(number + "是三位连续靓号");
            return true;
        }
        else {
            logger.info(number + "不是三位连续靓号");
        }
        p = Pattern.compile(RegexProperties.BEAUTIFUL_FOUR_ADD);
        m = p.matcher(number);
        if (m.matches()){
            logger.info(number + "是四位顺增靓号");
            return true;
        }
        else {
            logger.info(number + "不是四位顺增靓号");
        }
        p = Pattern.compile(RegexProperties.BEAUTIFUL_FOUR_DOWN);
        m = p.matcher(number);
        if (m.matches()){
            logger.info(number + "是四位顺减靓号");
            return true;
        }
        else {
            logger.info(number + "不是四位顺减靓号");
        }
        Random random = new Random();
        int i = random.nextInt(20);
        if (i > 3){
            logger.info(i + "模拟生成了靓号");
            return true;
        }else {
            logger.info(i + "通过");
        }
        return false;
    }

    public static void main(String[] args) {
//        String phone = "1288050276a";
//        checkPhone(phone);
        while (true){
            Scanner sc = new Scanner(System.in);
            String str = sc.next();
            isGoodNumber(str);
        }
    }
}
